<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="公司名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入公司名称"
          clearable
          size="default"
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="公司状态"
          clearable
          size="default"
          style="width: 240px"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <search-button @click="handleQuery" :perm="['system:company:search']" />
        <reset-button @click="resetQuery" :perm="['system:company:search']" />
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <add-button @click="handleAdd" :perm="['system:company:insert']" />
      <right-toolbar
        v-model:showSearch="showSearch"
        @query-table="getList"
        :perm="['system:company:search']"
      />
    </el-row>
    <el-table
      v-loading="loading"
      :data="companyList"
      row-key="companyId"
      default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="companyName" label="公司名称" min-width="180" />
      <el-table-column prop="companyCode" label="公司编码" min-width="80" />
      <el-table-column prop="companyShortname" label="简称" min-width="120" />
      <el-table-column
        prop="companyType"
        label="公司类型"
        :formatter="companyTypeFormat"
        width="120"
      />
      <el-table-column
        prop="companyLevel"
        label="公司级别"
        :formatter="companyLevelFormat"
        width="120"
      />
      <ExtColTableColumn tableName="sys_company" />
      <el-table-column
        prop="status"
        label="状态"
        align="center"
        :formatter="statusFormat"
        width="80"
      >
        <template #default="scope">
          <span :class="scope.row.status === 'N' ? 'text-danger' : ''">{{
            selectDictLabel(statusOptions, scope.row.status)
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="200px"
      >
        <template #default="scope">
          <text-add-button @click="handleAdd(scope.row)" :perm="['system:company:insert']" />
          <text-edit-button @click="handleUpdate(scope.row)" :perm="['system:company:update']" />
          <text-delete-button
            v-if="scope.row.parentId != 0"
            @click="handleDelete(scope.row)"
            :perm="['system:company:delete']"
            :subject="scope.row.companyName"
          />
        </template>
      </el-table-column>
    </el-table>
    <drag-dialog
      :title="title"
      :close-on-click-modal="false"
      v-model="open"
      width="800px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24" v-if="form.parentId !== 0">
            <el-form-item label="上级公司" prop="parentId">
              <el-tree-select v-model="form.parentId" :data="orgOptions" check-strictly
                :render-after-expand="false" :props="{ value: 'companyId', label: 'companyName', children: 'children' }"
                placeholder="请选择上级公司" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="公司名称" prop="companyName">
              <el-input v-model="form.companyName" placeholder="请输入公司名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公司编码" prop="companyCode">
              <el-input v-model="form.companyCode" placeholder="请输入公司编码" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="简称" prop="shortname">
              <el-input v-model="form.companyShortname" placeholder="请输入公司简称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="显示排序" prop="orderNum">
              <el-input-number v-model="form.orderNum" controls-position="right" :min="1" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="公司类型">
              <el-radio-group v-model="form.companyType">
                <el-radio
                  v-for="dict in companyTypeOptions"
                  :key="dict.dictValue"
                  :value="dict.dictValue"
                >{{ dict.dictLabel }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="公司级别">
              <el-radio-group v-model="form.companyLevel">
                <el-radio
                  v-for="dict in companyLevelOptions"
                  :key="dict.dictValue"
                  :value="dict.dictValue"
                >{{ dict.dictLabel }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="负责人" prop="leader">
              <el-input v-model="form.leader" placeholder="请输入负责人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入联系电话" maxlength="11" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="form.email" placeholder="请输入邮箱" maxlength="50" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio v-for="dict in statusOptions" :key="dict.value" :value="dict.dictValue">{{
                  dict.dictLabel
                }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <ExtColFormItem tableName="sys_company" :form="form" :span="12" />
      </el-form>
      <template #footer>
        <dialog-footer @ok="submitForm" @cancel="cancel" />
      </template>
    </drag-dialog>
  </div>
</template>

<script>
import {
  listCompany,
  getCompany,
  addCompany,
  updateCompany,
  delCompany
} from '@base/api/base/company'

export default {
  name: 'BaseCompany',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格树数据
      companyList: [],
      // 机构树选项
      orgOptions: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 状态数据字典
      statusOptions: [],
      // 公司类型下拉框
      companyTypeOptions: [],
      // 公司级别下拉框
      companyLevelOptions: [],
      // 查询参数
      queryParams: {
        companyName: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // parentId: [
        //   { required: true, message: "上级公司不能为空", trigger: "blur" }
        // ],
        companyName: [{ required: true, message: '公司名称不能为空', trigger: 'blur' }],
        companyCode: [{ required: true, message: '公司编码不能为空', trigger: 'blur' }],
        orderNum: [{ required: true, message: '菜单顺序不能为空', trigger: 'blur' }],
        email: [
          {
            type: 'email',
            message: "'请输入正确的邮箱地址",
            trigger: ['blur', 'change']
          }
        ],
        phone: [
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: '请输入正确的手机号码',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    //初始化查询
    this.getList()
    //初始化数据字典
    this.getDicts('sys_normal_disable').then((response) => {
      this.statusOptions = response.data
    })
    this.getDicts('sys_company_type').then((response) => {
      this.companyTypeOptions = response.data
    })
    this.getDicts('sys_company_level').then((response) => {
      this.companyLevelOptions = response.data
    })
  },
  methods: {
    /** 查询公司列表 */
    getList() {
      listCompany(this.queryParams).then((response) => {
        // this.companyList = this.handleTree(response.data);
        this.companyList = response.data
        // this.total = response.data.total;
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 字典状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status)
    },
    companyTypeFormat(row, column) {
      return this.selectDictLabel(this.companyTypeOptions, row.companyType)
    },
    companyLevelFormat(row, column) {
      return this.selectDictLabel(this.companyLevelOptions, row.companyLevel)
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        companyId: undefined,
        parentId: undefined,
        companyName: undefined,
        companyCode: undefined,
        companyShortname: undefined,
        orderNum: 1,
        leader: undefined,
        phone: undefined,
        email: undefined,
        companyType: '',
        status: 'Y'
      }
      this.resetForm('form')
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset()
      if (row != undefined) {
        this.form.parentId = row.companyId
      }
      this.open = true
      this.title = '添加公司'
      // this.form.companyId = row.companyId;
      listCompany().then((response) => {
        // this.orgOptions = this.handleTree(response.data, "companyId");
        this.orgOptions = response.data
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      getCompany(row.companyId).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改公司'
      })
      listCompany(row.companyId).then((response) => {
        // this.orgOptions = this.handleTree(response.data, "companyId");
        this.orgOptions = response.data
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.companyId != undefined) {
            if (this.form.parentId != this.form.companyId) {
              updateCompany(this.form).then((response) => {
                if (response.data === 1) {
                  this.msgSuccess('修改成功')
                  this.open = false
                  this.getList()
                }
              })
            } else {
              this.msgError('不能选自己作为父级公司')
            }
          } else {
            addCompany(this.form).then((response) => {
              if (response.data === 1) {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              }
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      delCompany(row.companyId)
        .then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
        .catch(function () {})
    }
  }
}
</script>
